package parkshop.pmcourse.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.OrderComment;
import parkshop.pmcourse.domain.Orderdetail;
import parkshop.pmcourse.domain.Product;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.mapping.OrderCommentMapper;
import parkshop.pmcourse.mapping.OrderdetailMapper;
import parkshop.pmcourse.mapping.ProductMapper;
import parkshop.pmcourse.service.ProductService;
import parkshop.pmcourse.utils.ConstantUtil;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductMapper productMapper;
	
	@Autowired 
	private OrderdetailMapper orderdetailMapper;
	@Autowired
	private OrderCommentMapper orderCommentMapper;
	
	@Override
	public Product getProductById(long id) {
		return productMapper.selectByPrimaryKey(id);
	}
	
	

	@Override
	public List<Product> getTopProductsByAdmin(int pageNumber){
		List<Product> productList=null;
		PageBean pageBean=new PageBean();
		pageBean.setStartindex((pageNumber-1)*ConstantUtil.mainPageProductSize);
		pageBean.setPageSize(ConstantUtil.mainPageProductSize);
		List<Orderdetail> orderdetailList =  orderdetailMapper.selectTopProducts(pageBean);// 获取最佳销量的产品
		int count = productMapper.selectProductCount();// 获取当前超市所有商品的数量
		/*
		 * 产品数量够显示一页的前提下，
		 * 销售量较好的产品数量超过一页，
		 * 则按商品销量的 top 排名分页显示，
		 * 否则按数据库中的索引序列显示
		 * 在 parknshop刚上线时，没有销量，则显示最前面一页的商品
		 */
		if(count>=ConstantUtil.mainPageProductSize&&orderdetailList!=null&&orderdetailList.size()==ConstantUtil.mainPageProductSize){
			productList = new ArrayList<Product>();
		for (Orderdetail orderdetail : orderdetailList) {
			Product product = productMapper.selectByPrimaryKey(orderdetail.getProductId());
			if(product!=null){
			productList.add(product);
			}
		}
		}else{			
			pageBean = new PageBean(pageNumber, ConstantUtil.mainPageProductSize, count);
			productList = productMapper.getProductsByPage(pageBean);
		}
		return productList;
	}
	
	@Override
	public PageBean getProductsByPage(int pageNumber,Long shopId) {
		PageBean pageBean = new PageBean();
		pageBean.setStartindex((pageNumber-1)*ConstantUtil.mainPageProductSize);
		pageBean.setPageSize(ConstantUtil.mainPageProductSize);
		List<Product> productList=null;
		int count=1;
		if(shopId==ConstantUtil.adminShopMark){
			count = productMapper.selectProductCount();
			productList=getTopProductsByAdmin(pageNumber);
			
		}else{
			// 根据商店名字查询 product
			pageBean.setId(shopId);
			productList = productMapper.getProductsPageByShopId(pageBean);
			count=productMapper.selectProductCountByShopId(shopId);
		}
		pageBean.setRecordList(productList);
		pageBean.setCurrentPage(pageNumber);
		pageBean.setRecordCount(count);
		pageBean.setPageCount(count%ConstantUtil.mainPageProductSize==0?count/ConstantUtil.mainPageProductSize:count/ConstantUtil.mainPageProductSize+1);// 16 个正好是4 行商品
		return pageBean;
	}

	@Override
	public void saveProduct(Product product) {
		productMapper.insertProduct(product);
	}



	@Override
	public boolean deleteProductById(int id) {
		// TODO Auto-generated method stub
		int rows=productMapper.deleteByPrimaryKey((long) id);
		return rows==0?true:false;
	}



	@Override
	public boolean updateProduct(Product product) {
		// TODO Auto-generated method stub
		if(product==null||product.getId()==null)
			return false;
		int rows=productMapper.updateByPrimaryKeySelective(product);
		return rows==1?true:false;
	}



	@Override
	public PageBean getProductsByType(int pageNumber, String productType,Long shopId) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean();
		pageBean.setStartindex((pageNumber-1)*ConstantUtil.pageSize);
		pageBean.setPageSize(ConstantUtil.pageSize);
		pageBean.setType(productType);
		pageBean.setId(shopId);
		List<Product> list=null;
		int count=0;
		if(ConstantUtil.adminShopMark==shopId){
		 list=productMapper.selectAllProductsByType(pageBean);
		count=productMapper.selectAllProductCountByType(productType);
		}else{
			list=productMapper.selectProductsByTypeAndShopId(pageBean);
			count=productMapper.selectProductCountByTypeAndShopId(pageBean);
		}
		
		pageBean.setRecordCount(count);
		pageBean.setRecordList(list);
		pageBean.setCurrentPage(pageNumber);
		pageBean.setPageCount(count%ConstantUtil.pageSize==0?count/ConstantUtil.pageSize:count/ConstantUtil.pageSize+1);
		
		return pageBean;
	}



	@Override
	public PageBean getProductNewComment(Long productId,int pageNumber) {
		// TODO Auto-generated method stub
		int count=orderCommentMapper.selectCommentCountByProduct(productId);
		PageBean pageBean=new PageBean();
		pageBean.setId(productId);
		int startindex=count-ConstantUtil.pageSize>0?count-ConstantUtil.pageSize:0;// 如果评论数量足够，那么读取最新的一页评论。在MySQL中即最后几条
		pageBean.setStartindex(startindex);
		pageBean.setPageSize(ConstantUtil.pageSize);
		
		List<OrderComment> list=orderCommentMapper.selectPageCommentByProductId(pageBean);
		pageBean.setRecordList(list);
		pageBean.setRecordCount(count);
		pageBean.setCurrentPage(pageNumber);
		pageBean.setPageCount(count%ConstantUtil.pageSize==0?count/ConstantUtil.pageSize:count/ConstantUtil.pageSize+1);
		
		
		return pageBean;
	}


	
}
